<?php
use yii\helpers\Inflector ;
$className = self::$console->className ;
$classNameArr = explode('/',$className) ;
$trueClassName = array_pop($classNameArr) ;
if(!empty($classNameArr)){
    $classNameID = implode('/',$classNameArr).'/'.Inflector::camel2id($trueClassName) ;
}else{
    $classNameID = Inflector::camel2id($trueClassName) ;
}


$elem_id = str_replace('/','_',$view).'_save' ;

$fileds = '' ;
foreach($colmns as $col){
    if($col['Key'] != 'PRI'){
        $fileds .= "                {$col['Field']}:'',\n";
    }
}


$validates = '' ;
foreach($colmns as $col){
    if($col['Key'] != 'PRI'){
        $validates .= "                {$col['Field']}:[Va.required],\n";
    }
}

?>


<div id="<?=$elem_id?>">
    <form>
        <zrow>
<?php

$newColmns = array_filter($colmns,function($v){
    return $v['Key'] !== 'PRI' ;
});
$newColmns = array_values($newColmns) ;
foreach ($newColmns as $k => $col){
    $comment = $col['Comment'] ?: $col['Field'] ;
    echo <<<ROW

            <zcol :md="6">
                <zinput label="{$comment}" name="{$col['Field']}" :ctx="this"/>
            </zcol>

ROW;
    if($k % 2 == 1){
echo <<<ZOL
        </zrow>


        <zrow>
ZOL;

    }
}
?>
        </zrow>

        <a @click="submit" class="btn btn-primary">提交</a>
    </form>

</div>

<script>
    new Vue({
        el:'#<?=$elem_id?>',
        mounted:function(){
            if(this.editData){
                copyProperty(this.editData , this.form)
            }
            this.state.status = 'mounted'
        },
        data:{
            state:{
                status:''
            },
            form:{
<?=$fileds?>
            },
            validate:{
<?=$validates?>
            },
            editData:<?php echo '<?=$data?>'?>

        },
        methods:{
            submit:function(){

                if(!validateAll(this)){
                    return
                }

                var id = App.url().param('id') || '' ;

                zhttp.post('<?=$classNameID?>/save?id='+id,this.form,function (d) {
                    if(d.code == 0){
                        success('操作成功')
                        //Loader.getDialog('<?=$classNameID?>/save').close()
                        go('<?=$classNameID?>/index')
                    }else{

                    }
                    return false ;
                })
            }
        }
    })
</script>